import store from "@/store";
import {service} from "@/utils";
import {i18n} from '@/locales'
import {FormItemContext} from "element-plus";

const {t} = i18n.global as any;

export const validator = async (rule: any, value: any, callback: (e?: Error) => any) => {
    if (rule.required && (!value || value.length === 0)) {
        callback(new Error(t(`form.required`)));
        return;
    }
    let type = '', params: any;
    if (rule.group === 'quartz') {
        // 定时任务
        switch (rule.field) {
            case 'cronExpression':
                type = await service.get('/job/check/cron?cron=' + value).then(({data}) => data? '' : 'format');
                break;
        }
    } else if (rule.group === 'password') {
        // 修改密码
        switch (rule.field) {
            case 'password':
                const {user} = store.userStore;
                type = await service.get('/user/check/password', {username: user.username, password: value}).then(({data}) => data? '':'error');
                break;
            case 'newPassword':
                if (value.length < 6) {
                    type = 'format';
                    params = {min: 6}
                }
                break
            case 'confirmPassword':
                if (value !== rule.password()) {
                    type = 'error'
                }
                break;
        }
    } else if (rule.group === 'ssh') {
        switch (rule.field) {
            case 'host':
                const ipRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
                if (!ipRegex.test(value)) {
                    type = 'format';
                }
                break;
        }

    } else {
        // 通用
        switch (rule.field) {
            case 'username':
                if (value.length < 4 || value.length > 10) {
                    type = 'format';
                    params = {min: 4, max: 10}
                } else {
                    type = await service.get('/user/check/username', {username: value}).then(({data}) => data ? '' : 'exist');
                }
                break;
            case 'email':
                if (value && !value.isEmail()) type = 'format'
                break;
            case 'mobile':
                if (value && !value.isPhone()) type = 'format';
                break;
        }
    }
    return type.length > 0 ? callback(new Error(t(`form.${rule.group}.${rule.field}.${type}`, params))) : callback();
}

function toBusinessFormValidator(group: string) {

}

export const getRules = (fields: FormItemContext[], group = 'system') => {
    if (!fields || !fields.length) return {}
    return fields.filter((field) => field.required !== undefined).reduce((data: { [key:string]:any }, item) => {
        if (item.prop) {
            data[item.prop as string] = {required: item.required, trigger: 'blur', group: group, form: data, validator: validator};
        }
        return data;
    }, {})
}